1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.collect.testing.Helpers;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26
27 import java.lang.reflect.Method;
28 import java.util.Collection;
29
30
31
32
33
34
35 @GwtCompatible(emulated = true)
36 public class SetHashCodeTester<E> extends AbstractSetTester<E> {
37 public void testHashCode() {
38 int expectedHashCode = 0;
39 for (E element : getSampleElements()) {
40 expectedHashCode += ((element == null) ? 0 : element.hashCode());
41 }
42 assertEquals(
43 "A Set's hashCode() should be the sum of those of its elements.",
44 expectedHashCode, getSet().hashCode());
45 }
46
47 @CollectionSize.Require(absent = CollectionSize.ZERO)
48 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
49 public void testHashCode_containingNull() {
50 Collection<E> elements = getSampleElements(getNumElements() - 1);
51 int expectedHashCode = 0;
52 for (E element : elements) {
53 expectedHashCode += ((element == null) ? 0 : element.hashCode());
54 }
55
56 elements.add(null);
57 collection = getSubjectGenerator().create(elements.toArray());
58 assertEquals(
59 "A Set's hashCode() should be the sum of those of its elements (with "
60 + "a null element counting as having a hash of zero).",
61 expectedHashCode, getSet().hashCode());
62 }
63
64
65
66
67
68
69
70 @GwtIncompatible("reflection")
71 public static Method[] getHashCodeMethods() {
72 return new Method[]{
73 Helpers.getMethod(SetHashCodeTester.class, "testHashCode"),
74 Helpers.getMethod(SetHashCodeTester.class, "testHashCode_containingNull") };
75 }
76 }